package com.keliangliu.demo.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

@SuppressWarnings("ALL")
public class MLock implements Lock {

    private volatile Integer state = 0;

    @Override
    public void lock() {
        synchronized (this) {
            while (state != 0) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            state = 1;
        }
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    @Override
    public boolean tryLock() {
        return false;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void unlock() {
        synchronized (this) {
            state = 0;
            notifyAll();
        }
    }

    @Override
    public Condition newCondition() {
        return null;
    }

}
